package com.dragon.concurrent.demo.demo;

import java.util.concurrent.TimeUnit;

public class SynchronizedBlocked implements Runnable {

    public synchronized void f() {
        System.out.println("Trying to call f()");
        while (true) {
            Thread.yield();
        }
    }

    public SynchronizedBlocked() {
        new Thread(() -> {
            f();
        }).start();
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().isInterrupted());
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("中断线程");
                break;
            }  else {
                f();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SynchronizedBlocked synchronizedBlocked = new SynchronizedBlocked();
        Thread thread = new Thread(synchronizedBlocked);
        thread.start();
        TimeUnit.SECONDS.sleep(2L);
        thread.interrupt();
        System.out.println("finish main");
    }
}
